package com.srpingde.hhh.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.springde.hhh.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.bean.TopDataBean;
import com.srpingde.hhh.jiekou.HHHIml;

	public class TopGridViewAdaPter extends BaseAdapter {
	    	Context context;
	    	gridViewHolder holder = null;
	    	List<TopDataBean> list ;
	    	public TopGridViewAdaPter(Context context,List<TopDataBean> list) {
	    		super();
	    		this.context = context;
	    		this.list = list;
	    	}
	    	@Override
	    	public int getCount() {
	    		// TODO Auto-generated method stub
	    		return list.size();
	    	}

	    	@Override
	    	public Object getItem(int arg0) {
	    		// TODO Auto-generated method stub
	    		return arg0;
	    	}

	    	@Override
	    	public long getItemId(int position) {
	    		// TODO Auto-generated method stub
	    		return position;
	    	}

	    	@Override
	    	public View getView(int position, View convertView, ViewGroup parent) {
	    		// TODO Auto-generated method stub
	    		if(convertView == null){
	    			convertView = LayoutInflater.from(context).inflate(R.layout.topitem, null);
	    			holder = new gridViewHolder();
	    			holder.imageView = (ImageView) convertView.findViewById(R.id.top_item_iv);
	    			holder.item_price = (TextView) convertView.findViewById(R.id.item_price);
	    			holder.item_name = (TextView) convertView.findViewById(R.id.item_name);
	    			convertView.setTag(holder);
	    		}else{
	    			holder = (gridViewHolder) convertView.getTag();
	    		}
	    		itemViewLyout(holder.imageView);
	    		Log.e("mytag", HHHIml.httpHeader+list.get(position).getUrl());
	    		final ImageView kvShow = holder.imageView;
	    		ImageLoader.getInstance().loadImage(HHHIml.httpHeader+list.get(position).getUrl(), new SimpleImageLoadingListener(){
	    			@Override
	    			public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {
	    				BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
	    				//kvShow.setImageBitmap(bitmap);
	    				kvShow.setBackgroundDrawable(bitmapDrawable);
	    			}
	    		});
	    		holder.item_name.setText(list.get(position).getName());
	    		holder.item_price.setText("  ￥"+list.get(position).getPrice());
	    		return convertView;
	    	}
	    	class gridViewHolder{
	    		TextView new_tv;
	    		TextView tuijian_tv;
	    		ImageView imageView;
	    		TextView item_price,item_name;
	    	}
	    	 private void itemViewLyout(ImageView imageView){
	 	    	int layout_wh = (int) (HhhApp.getInstance().getDisplayHightAndWightPx()[1]*0.5);
	 	    	RelativeLayout.LayoutParams layoutParams = (LayoutParams) imageView.getLayoutParams();
	 	    	layoutParams.height = (int) ((HhhApp.getInstance().getDisplayHightAndWightPx()[0]- layout_wh*0.15-HhhApp.getInstance().title_h()-HhhApp.getInstance().title_h()-HhhApp.getInstance().getDisplayHightAndWightPx()[1] * 0.4-11)/2);
	 	    	layoutParams.width = layout_wh;
	 	    	imageView.setLayoutParams(layoutParams);
	 	    }
}
